<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2018/5/4
 * Time: 17:50
 */

namespace app\admin\controller\sunfc;


use app\admin\controller\Base;
use think\Db;

class Users extends Base
{
    /**
     * 获取会员列表（GET）
     * URL：admin/sunfc.users/index
     */
    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $res = Db::name('dd_users')
                ->page($page, $limit)->select();

            foreach ($res as $index => $re) {
               // $res[$index]['status2'] = $status[$re['status']];
                if($res[$index]['update_time'] == null){
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                }else{
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                    $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
                }

            }
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);

        }else{
            $count =  Db::name('dd_users')->count();
            $this->assign('count',$count);
            return $this->fetch();
        }

    }
    /**
     * 会员条件查询（GET）
     * URL：admin/sunfc.users/cases_search_index
     */
    public function cases_search_index()
    {
        $log = Db::name('dd_users');

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        // 判断姓名是否为空;

        if(input('user_id')!=null){

                $map["iw_dd_users.uid"] =  trim(input('user_id'));
                $user = trim(input('user_id')).'%';
                $res= $log
                    ->page($page, $limit)
                    ->where("realname",'like',$user)
                    ->select();
            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];

                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }
                $total = $log->page($page, $limit)
                    ->where("realname",'like',$user)
                    ->count();
                return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

        }else{
            $map["iw_dd_users.tel"] = input('tel');
            $tel = input('tel').'%';
            $res= $log
                ->where('tel','like',$tel)
                ->page($page, $limit)
                ->select();
            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];

                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }
            $total = $log->page($page, $limit)
                ->where($map)
                ->count();
            return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
        }




    }
    /**
     * 查看用户详情（GET）
     * URL：admin/sunfc.users/look
     */
    public function look()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        $res = Db::name('dd_users')->find($id);
        $this->assign('item', $res);
        return $this->fetch();

    }

    /**
     * 设置用户状态（GET）
     * URL：admin/sunfc.users/set_status
     */

    public function set_status()
    {
        $data = input('');
       // dump($data);die();
        $ids = $data['ids'];
        $status = $data['status'];

        if (!$ids) {
            $this->error('异常');
        }

        Db::name('dd_users')->where('uid','in',$ids)->update(['status' => $status]);
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $res = Db::name('dd_users')
            ->page($page, $limit)->select();

        foreach ($res as $index => $re) {
            // $res[$index]['status2'] = $status[$re['status']];
            if($res[$index]['update_time'] == null){
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }else{
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
            }

        }
        return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

    }
    /**
     * 会员认证（GET）
     * URL：admin/sunfc.users/user_auth
     */
    public function user_auth()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $res = Db::name('dd_users')
                ->where('auth_user','neq',0)
                ->page($page, $limit)
                ->select();

            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];
                if($res[$index]['update_time'] == null){
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                }else{
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                    $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
                }

            }
            return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

        }else{
            $count =  Db::name('dd_users')->where('auth_user',1)->count();
            $this->assign('count',$count);
            return $this->fetch();
        }
    }
    /**
     * 查看会员详情（GET）
     * URL：admin/sunfc.users/look
     */
    public function look2()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        $res = Db::name('dd_users')->find($id);
        $this->assign('item', $res);
        return $this->fetch();

    }
    /**
     * 设置会员审核状态（GET）
     * URL：admin/sunfc.users/look
     */

    public function set_status2()
    {
        $data = input('');
        // dump($data);die();
        $ids = $data['ids'];
        $status = $data['status'];

        if (!$ids) {
            $this->error('异常');
        }

        Db::name('dd_users')->where('uid','in',$ids)->update(['auth_user' => $status]);
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $res = Db::name('dd_users')
            ->where('auth_user','neq',0)
            ->page($page, $limit)
            ->select();

        foreach ($res as $index => $re) {
            // $res[$index]['status2'] = $status[$re['status']];
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
        }
        return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

    }
    /**
     * 司机认证（GET）
     * URL：admin/sunfc.users/driver_auth
     */
    public function driver_auth()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $res = Db::name('dd_users')
                ->where('auth_driver','neq',0)
                ->page($page, $limit)
                ->select();

            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];
                if($res[$index]['update_time'] == null){
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                }else{
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                    $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
                }

            }
            return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

        }else{
            $count =  Db::name('dd_users')->where('auth_driver','neq',0)->count();
            $this->assign('count',$count);
            return $this->fetch();
        }
    }
    /**
     * 查看司机详情（GET）
     * URL：admin/sunfc.users/look
     */
    public function look3()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        $res = Db::name('dd_users')->find($id);
        $this->assign('item', $res);
        return $this->fetch();

    }
    /**
     * 设置司机审核状态（GET）
     * URL：admin/sunfc.users/look
     */

    public function set_status3()
    {
        $data = input('');
        // dump($data);die();
        $ids = $data['ids'];
        $status = $data['status'];
        $type =  $data['type'];
        if (!$ids) {
            $this->error('异常');
        }

        Db::name('dd_users')->where('uid','in',$ids)->update(['auth_driver' => $status,'type'=>$type]);
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $res = Db::name('dd_users')
            ->where('auth_driver','neq',0)
            ->page($page, $limit)
            ->select();

        foreach ($res as $index => $re) {
            // $res[$index]['status2'] = $status[$re['status']];
            if($res[$index]['update_time'] == null){
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }else{
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
            }

        }
        return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

    }


    /**
     * 粉丝留言
     */
    public function chat()
    {
        if (request()->isAjax()) {
            $page       = input('page')     ? input('page')     : 1;
            $limit      = input('limit')    ? input('limit')    : 10;

            $res = Db::name('dd_robot')->page($page, $limit)->order('id desc')->select();

            foreach ($res as $index => $re) {
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }

            return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

        }else{

            $count =  Db::name('dd_robot')->count();
            $this->assign('count',$count);

            return $this->fetch();
        }
    }

}